/*
 * =====================================================================================
 *
 *       Filename:  ftp.hpp
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  11.08.2012 00:27:46
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  YOUR NAME (), 
 *   Organization:  
 *
 * =====================================================================================
 */

#ifndef FTP_HPP_
#define FTP_HPP_

#include <sstream>
#include <fstream>
#include <string>
#include <ev++.h>

#include "signal.hpp"
#include "sslsocket.hpp"
#include "globals.hpp"
#include "drosera.pb.hpp"

#define ERRORDISCON	-1
#define ERRORWRITE	-2
#define ERRORINFO	-3
#define ERRORCORRUPT	-4
#define NOERROR		0


namespace Drosera {

struct File {
	long fsize;
	std::string md5;
	std::string name;
	bool status;
};

class Ftp : public SSLSocket {

	public:
		Ftp ( SSL* );
		~Ftp ( void );

		unsigned int getFtpId ( void );
		int sendFile ( std::string& path, std::string& );
		int sendFile ( char *, char * );

		Signal<void, unsigned int>& signalUpdate ( void );
		Signal<void, unsigned int, int>& signalDisconnected ( void );


	private:
		ev::io m_io;
		unsigned int m_id;
		char m_tmp[L_tmpnam];
		struct File m_info;

		Signal<void, unsigned int> m_signalUpdate;
		Signal<void, unsigned int, int> m_signalDisconnected;

	protected:
		void checkSocket ( ev::io&, int );
		static bool checkMD5 ( std::string&, std::string& );
		static bool checkMD5 ( char*, std::string& );

		int writeBuffer ( std::string& );
		int checkFile ( void );
		int getFileInfo ( std::string& );
	
};
}

#endif
